package com.salesmanager.web.admin.controller;

import com.salesmanager.core.business.customer.service.CustomerService;
import com.salesmanager.core.business.user.model.User;
import com.salesmanager.core.business.user.service.UserService;
import com.salesmanager.core.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@Controller
public class AdminController {

    @Autowired
    private CustomerService customerService;

	@Autowired
	UserService userService;
	
	@RequestMapping(value={"/home","/","index"}, method=RequestMethod.GET)
	public String displayDashboard(Model model, HttpServletRequest request, HttpServletResponse response) throws Exception {

		//display menu
		Map<String,String> activeMenus = new HashMap<String,String>();
		activeMenus.put("home", "home");
		
		model.addAttribute("activeMenus",activeMenus);

		String sCurrentUser = request.getRemoteUser();
		User currentUser = userService.getByUserName(sCurrentUser);

        String startTime = DateUtil.TodaystartTime();
        String endTime = DateUtil.TodayEndTime();

        //用户总数
        int totalCount = customerService.getCustomerCount(null, null);
        //今日用户数
        int todayCount = customerService.getCustomerCount(startTime, endTime);

        model.addAttribute("totalCount",totalCount);
        model.addAttribute("todayCount",todayCount);

		model.addAttribute("user", currentUser);

		if (!"admin".equals(sCurrentUser)){
			return ControllerConstants.Tiles.channelHome;
		}

		return ControllerConstants.Tiles.adminDashboard;
	}
	



}
